package com.tv.cms.deviceInfo.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tv.cms.deviceInfo.model.DeviceInfo;
import com.tv.cms.deviceInfo.service.DeviceInfoService;
import com.tv.cms.system.account.model.Account;
import com.tv.cms.system.admin.model.Admin;
import com.tv.cms.system.admin.service.AdminService;
import com.tv.cms.system.operator.model.Operator;
import com.tv.cms.utils.Md5encrypt;
import com.tv.cms.utils.Result;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * TODO
 *    设备管理
 *
 * @author
 * @date
 **/
@RestController
@RequestMapping("deviceInfo")
public class DeviceInfoController {

    @Resource
    private DeviceInfoService service;
    @Resource
    private AdminService adminService;

    /**
     * 设备列表
     *
     * @param deviceInfo
     * @return
     */
    @PostMapping("list")
    public Result<Page<DeviceInfo>> list(@RequestBody DeviceInfo deviceInfo) {
        Admin admin = adminService.getById(StpUtil.getLoginIdAsInt());
        MPJLambdaWrapper<DeviceInfo> wrapper = new MPJLambdaWrapper<DeviceInfo>()
                .selectAll(DeviceInfo.class)
                .eq(deviceInfo.getStatus() != null, DeviceInfo::getStatus, deviceInfo.getStatus())
                .eq(StringUtils.hasText(deviceInfo.getMac()), DeviceInfo::getMac, deviceInfo.getMac())
                .eq(admin.getRoleId() != 1, DeviceInfo::getOperator, admin.getUserName())
                .eq(DeviceInfo::getType, 0)
                .like(StringUtils.hasText(deviceInfo.getAuthCode()), DeviceInfo::getAuthCode, deviceInfo.getAuthCode())
                .like(StringUtils.hasText(deviceInfo.getComment()), DeviceInfo::getComment, deviceInfo.getComment())
                .orderByDesc(DeviceInfo::getCreateTime);
        Page<DeviceInfo> objectPage = new Page<>(deviceInfo.getPage(), deviceInfo.getLimit());
        Page<DeviceInfo> page = service.selectPageList(objectPage, wrapper);
        return new Result<Page<DeviceInfo>>().OKDATA(page);
    }


    /**
     * 更新设备状态
     *
     * @param deviceInfo
     * @return
     */
    @PutMapping("updateStatus")
    public Result updateStatus(@RequestBody DeviceInfo deviceInfo) {
        if (deviceInfo.getId() == null || deviceInfo.getStatus() == null) {
            return Result.FAILURE(400, "参数错误");
        }
        Admin admin = adminService.getById(StpUtil.getLoginIdAsInt());
        DeviceInfo byId = service.getDataById(deviceInfo.getId());
        if(admin.getRoleId() != 1   && !admin.getUserName().equals(byId.getOperator())) {
            return Result.FAILURE();
        }
        if (deviceInfo.getStatus() == 1) {
            if (byId.getActivityTime() == null) {
                deviceInfo.setStatus(0);
            }
        }

        deviceInfo.setLockOperator(admin.getUserName());
        boolean update = service.updateStatus(deviceInfo);
        return update ? Result.OK() : Result.FAILURE();
    }

    /**
     * 批量更新设备状态
     *
     * @param deviceInfo
     * @return
     */
    @PutMapping("updateStatusBatch")
    public Result updateStatusBatch(@RequestBody DeviceInfo deviceInfo) {
        if (!StringUtils.hasText(deviceInfo.getIds()) || deviceInfo.getStatus() == null) {
            return Result.FAILURE(400, "参数错误");
        }
        Admin admin = adminService.getById(StpUtil.getLoginIdAsInt());
        List<DeviceInfo> list = service.selectDataList(new MPJLambdaWrapper<DeviceInfo>().in(DeviceInfo::getId, Arrays.asList(deviceInfo.getIds().split(","))).eq(admin.getRoleId() != 1, DeviceInfo::getOperator, admin.getUserName()));
        for (DeviceInfo info : list) {
            info.setStatus(deviceInfo.getStatus());
            info.setLockOperator(admin.getUserName());
            if (deviceInfo.getStatus() == 1) {

                if (info.getActivityTime() == null) {
                    info.setStatus(0);
                }
            }

            service.updateStatus(info);

        }
       /* Admin admin = adminService.getById(StpUtil.getLoginIdAsInt());
        deviceInfo.setLockOperator(admin.getUserName());
        String[] split = deviceInfo.getIds().split(",");
        for (String s : split) {
            deviceInfo.setId(Integer.parseInt(s));
            service.updateStatus(deviceInfo);
        }*/
        return Result.OK();
    }

}
